<?
	Class Search extends Aerodrome {

		public function find($keywords, $handler) {

			// parse the search string
		    $search_string = explode(" ", $keywords);
		    $keyword_count = count($search_string);

		    // query
		    $query = "SELECT * from aerodromes WHERE ";
		    $ident_clause = "ident IN ";
		    $name_clause = "OR name IN ";

		    foreach ($search_string as $key => $value) {
		    	$ident_clause .= ($key == 0) ? "(" : "";
		    	$ident_clause .= ($key < $keyword_count-1) ? "'" . $value . "'," : "'" . $value . "') ";

		    	$name_clause .= ($key == 0) ? "(" : "";
		    	$name_clause .= ($key < $keyword_count-1) ? "'" . $value . "'," : "'" . $value . "') ";
		    }

		    $query .= $ident_clause;
		    //$query .= $name_clause;

			// gather all
			$aerodromes = array();

			// populate aerodrome data
		    if ($result = $handler->query($query, MYSQLI_USE_RESULT)) {

		        while($row = $result->fetch_row()) { 

		        	// New aerodrome
					$aerodrome = new Aerodrome();
					$aerodrome->title = $row[3];
					$aerodrome->code = $row[1];
					$aerodrome->location = array('lat' => $row[5], 'lon' => $row[4], 'elevation' =>	$row[6]);
					$aerodrome->country = $row[8];
					// runways
					$aerodrome->getRunways($handler);
					// frequencies
					$aerodrome->getFrequencies($handler);

					$aerodromes[] = $aerodrome;

		        }
		        
		        $result->close();

		    } else {

		    	echo "No results";

		    }

		    return $aerodromes;

		}


		public function getNameByIdent($ident) {
			return $ident;
		}

	}
?>